package jiaoheng.数据结构;

import java.util.Comparator;
import java.util.PriorityQueue;

public class s4队列 {
}
// 暴力实现
class SlidingWindow{
    // 滑动窗口要填满值，返回从开始到最后每一步移动后，滑动窗口的最大值
    public int[] maxSlidingWindow(int[] nums,int k){
        //需要返回的最大值数组
        int[] result = new int[nums.length - k+1];
        // 一共移动的次数，窗口的起始位置
        for(int i=0;i<nums.length - k+1;i++){
            int maxSlidingWindowVal = Integer.MIN_VALUE;
            for(int j=i;j<i+k;j++){
                if(maxSlidingWindowVal < nums[j]){
                    maxSlidingWindowVal = nums[j];
                    result[i] = maxSlidingWindowVal;
                }
            }
        }
        return result;
    }
}

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int n = nums.length;
        PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() {
            public int compare(int[] pair1, int[] pair2) {
                return pair1[0] != pair2[0] ? pair2[0] - pair1[0] : pair2[1] - pair1[1];
            }
        });
        for (int i = 0; i < k; ++i) {
            pq.offer(new int[]{nums[i], i});
        }
        int[] ans = new int[n - k + 1];
        ans[0] = pq.peek()[0];
        for (int i = k; i < n; ++i) {
            pq.offer(new int[]{nums[i], i});
            while (pq.peek()[1] <= i - k) {
                pq.poll();
            }
            ans[i - k + 1] = pq.peek()[0];
        }
        return ans;
    }
}
// 单调队列
